package wowraidmanager.client.wishlist.user;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.client.service.wishlist.MemberServiceAsync;
import wowraidmanager.client.wishlist.utils.CheckError;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CreateSubuser extends BaseComposite{

    private static CreateSubuserUiBinder uiBinder = GWT.create(CreateSubuserUiBinder.class);

    protected MemberServiceAsync memberService = GWT.create(MemberService.class);

    private UserStatus userStatus;

    private Member targetMember = null;

    interface CreateSubuserUiBinder extends UiBinder<Widget, CreateSubuser>{
    }

    @UiField
    Label guildName;

    @UiField
    Label realmName;

    @UiField
    TextBox memberName;

    @UiField
    Label memberAccount;

    @UiField
    Label order;

    @UiField
    Label type;

    @UiField
    Button okButton;

    @UiField
    Button resetButton;

    public CreateSubuser(UserStatus userStatus){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        setLabel();
        setButton();
    }

    private void setButton(){
        okButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setMember();

            }
        });

        resetButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                clearInfo();
            }
        });

    }

    protected void clearInfo(){
        memberName.setText("");
    }

    protected void setMember(){
        if(targetMember == null){
            targetMember = new Member();
        }

        String guildNameString = guildName.getText();
        String realmNameString = realmName.getText();
        String typeString = type.getText();
        String memberNameString = memberName.getText();
        String memberAccountString = memberAccount.getText();
        String orderString = order.getText();

        errorString = CheckError.checkCreateUser(guildNameString, realmNameString, memberNameString, memberAccountString, orderString);

        if(errorString.equals("")){
            targetMember.setGuild(guildNameString);
            targetMember.setRealm(realmNameString);
            targetMember.setMembertype(typeString);
            targetMember.setMemberName(memberNameString);
            targetMember.setMemberAccount(memberAccountString);
            targetMember.setOrder(Long.valueOf(orderString));

            memberService.setMember(targetMember, new AsyncCallback<Void>(){

                public void onSuccess(Void result){
                    setInfoMessage("メンバーを追加/更新しました。");
                    clearErrorMessage();
                }

                public void onFailure(Throwable caught){
                    clearInfoMessage();
                    setErrorMessage("更新に失敗しました。");
                }
            });
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }

    private void setLabel(){
        guildName.setText(userStatus.getMember().getGuild());
        realmName.setText(userStatus.getMember().getRealm());
        memberAccount.setText(userStatus.getMember().getMemberAccount());
    }

}
